package adv3;

import java.util.*;

/**
 * @author oldliu
 * @since 1.0
 */
public class MapTest {
    public static void main(String[] args) {
        Map<String,String> z=new HashMap<>();
        z.put("来","come");
        z.put("去","go");
        z.put("点头","yes");
        z.put("狗","dog");
        z.put("牛","cow");
        z.put("猫","cat");
        System.out.println(z.size());
        System.out.println(z);
        z.put("去","go along");//覆盖
        z.put("空",null);
        System.out.println(z);
        String c = z.get("猫");
        System.out.println(c);
        //删除
        String niu = z.remove("牛");
        System.out.println(niu);
        String d = z.getOrDefault("狗", "xiaogou");
        System.out.println("d="+d);
        //获得键  或者  值 的集合
        Set<String> set = z.keySet();
        Collection<String> values = z.values();
        System.out.println("Map迭代-1");
        for(String s:set){
            String v=z.get(s);
            System.out.println(s+"="+v);
        }
        System.out.println("Map迭代-2:");
        Set<Map.Entry<String, String>> er = z.entrySet();
        for(Map.Entry<String, String> e:er){
            System.out.println(e.getKey()+"="+e.getValue());
        }

        Scanner in=new Scanner(System.in);
        System.out.println("input key:");
        String key=in.next();
        //String value=z.get(key);
        //if(value==null){
        if(!z.containsKey(key)){
            System.out.println("没有找到");
        }else{
            String value=z.get(key);
            System.out.println(key+"对应的是："+value);
        }
    }
}
